package com.weirong.smartvault.backservice.controller.fragment_entity;

import com.weirong.smartvault.common.tools.constant.ThymleafVarName;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 于2017/11/25由 KFEB4 创建 .
 */
public class PagesIndex {
    private List<Integer> indexs;
    private Integer index;
    private long total;
    private Map<String,String> param;
    public PagesIndex(Integer index, long total,String ... param){
        Map<String,String> p = new HashMap<>();
        for (int i = 0; i < param.length / 2; i++) {
            p.put(param[i*2],param[i*2+1]);
        }
        indexs = new ArrayList<>();
        long start = 0;
        if (index> ThymleafVarName.pageCount/2)
            start = index-ThymleafVarName.pageCount/2;
        for (long i = start; i < total || i <= index; i++) {
            indexs.add((int) i);
            if (indexs.size()>ThymleafVarName.pageCount)
                break;
        }
        p.put("page", String.valueOf(index));
        this.param = p;
        this.index = index;
        this.total = total;
    }

    public List<Integer> getIndexs(){
        return indexs;
    }
    public Integer getIndex(){
        return index;
    }
    public boolean haveNext(){
        return this.getIndex()<this.getTotal()-1;
    }
    public long getTotal(){
        return total;
    }
    public String getParam(Integer index){
        param.put("page", String.valueOf(index));
        StringBuilder res = new StringBuilder("?");
        for(String key:param.keySet()){
            res.append(key).append("=").append(param.get(key)).append("&");
        }
        return res.substring(0, res.length() - 1);
    }
}
